/*
 *  @ProjectName: ISMS_Petrel_MCU
 *  @Copyright: 2016 HangZhou Hikvision System Technology Co., Ltd. All Right Reserved.
 *  @address: http://www.hikvision.com
 *  @Description: 本内容仅限于杭州海康威视系统技术公有限司内部使用，禁止转发.
 */

package com.zhoug.player3.core.utils;

import android.text.TextUtils;
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;


/**
 * <p>查询录像日期转换工具</p>
 *
 * @author 张华洋 2017/2/20 17:02
 * @version V1.1
 * @name CalendarUtil
 */
public class CalendarUtil {

    private static final String TAG = ">>>CalendarUtil";

    private static final String yyyy_MM_dd_T_HH_mm_SSSZ = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
    private static final String yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";
    private static final String yyyy_MM_dd_HH_mm = "yyyy-MM-dd HH:mm";

    private CalendarUtil() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    /**
     * 获取当天的开始时间
     *
     * @return Calendar
     */
    public static Calendar getDefaultStartCalendar() {
        Calendar currentCalendar = Calendar.getInstance();
        int year = currentCalendar.get(Calendar.YEAR);
        int month = currentCalendar.get(Calendar.MONTH);
        int day = currentCalendar.get(Calendar.DAY_OF_MONTH);
        currentCalendar.set(year, month, day, 0, 0, 0);
        return currentCalendar;
    }


    /**
     * 根据传入的时间点，计算当天的 （24时-1ms） 最后时刻对应的时间
     *
     * @param time 当天 最后时刻-1ms当天 最后时刻-1ms
     * @return 最后时刻
     */
    public static long getCurDayEndTime(long time) {
        if (0 >= time) {
            Log.e(TAG, "getCurDayEndTime : The long time < 0");
            return time;
        }
        Calendar curCalendar = Calendar.getInstance();
        curCalendar.setTimeInMillis(time);
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.set(Calendar.YEAR, curCalendar.get(Calendar.YEAR));
        endCalendar.set(Calendar.MONTH, curCalendar.get(Calendar.MONTH));
        endCalendar.set(Calendar.DAY_OF_MONTH, curCalendar.get(Calendar.DAY_OF_MONTH));
        endCalendar.set(Calendar.HOUR_OF_DAY, 23);
        endCalendar.set(Calendar.MINUTE, 59);
        endCalendar.set(Calendar.SECOND, 59);
        endCalendar.set(Calendar.MILLISECOND, 999);
        return endCalendar.getTimeInMillis();
    }


    /**
     * 将"2018-05-07T14:41:57.819+03:00"型转换为Calendar,注意将转换为手机设备所在时区的Calendar
     *
     * @param formatTime "2018-05-07T14:41:57.819+03:00"型time
     * @return Calendar
     */
    public static Calendar yyyy_MM_dd_T_HH_mm_SSSZToCalendar(String formatTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        if (TextUtils.isEmpty(formatTime)) {
            Log.e(TAG, "convertToCalendar : The formatting time is null");
            return calendar;
        }
        StringBuilder builder = new StringBuilder(formatTime);
        if(formatTime.length()>26){
            builder.deleteCharAt(26);
        }else{
            Log.d(TAG, "yyyy_MM_dd_T_HH_mm_SSSZToCalendar:formatTime="+formatTime);
        }
        SimpleDateFormat sdf = new SimpleDateFormat(yyyy_MM_dd_T_HH_mm_SSSZ, Locale.getDefault());
        try {
            Date date = sdf.parse(builder.toString());
            calendar.setTime(date);
            return calendar;
        } catch (ParseException e) {
            Log.e(TAG, "convertToCalendar : The time format must be 2018-05-07T14:41:57.819+03:00");
            return calendar;
        }
    }

    /**
     * 将"2018-05-07T14:41:57.819+03:00"型转换为Calendar,注意将转换为手机设备所在时区的Calendar
     *
     * @param formatTime "2018-05-07 14:41"型time
     * @return Calendar
     */
    public static long yyyy_MM_dd_HH_mmToLong(String formatTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        if (TextUtils.isEmpty(formatTime)) {
            Log.e(TAG, "convertToCalendar : The formatting time is null");
            return 0;
        }
        StringBuilder builder = new StringBuilder(formatTime);
        if(formatTime.length()>16){
            builder.deleteCharAt(16);
        }else{
            Log.d(TAG, "yyyy_MM_dd_T_HH_mm_SSSZToCalendar:formatTime="+formatTime);
        }
        SimpleDateFormat sdf = new SimpleDateFormat(yyyy_MM_dd_HH_mm, Locale.getDefault());
        try {
            Date date = sdf.parse(builder.toString());
            calendar.setTime(date);
            return calendar.getTimeInMillis();
        } catch (ParseException e) {
            Log.e(TAG, "convertToCalendar : The time format must be 2018-05-07T14:41:57.819+03:00");
            return 0;
        }
    }




    /**
     * 将Calendar格式化为"2018-05-07T14:41:57.819+03:00",转换为手机设备所在时区
     *
     * @param calender calenderTime
     * @return 格式化时间字符串
     */
    public static String calendarToyyyy_MM_dd_T_HH_mm_SSSZ(Calendar calender) {
        if (null == calender) {
            Log.e(TAG, "convertToString : The calender time is null");
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(yyyy_MM_dd_T_HH_mm_SSSZ, Locale.getDefault());
        StringBuilder builder = new StringBuilder(sdf.format(calender.getTime()));
        builder.insert(26, ":");
        return builder.toString();
    }


    /**
     * 将时间格式化为"2018-05-07T14:41:57.819+03:00",转换为手机设备所在时区
     *
     * @param time calenderTime
     * @return 格式化时间字符串
     */
    public static String timeToyyyy_MM_dd_T_HH_mm_SSSZ(long time) {
        if (0 >= time) {
            Log.e(TAG, "convertToString : time 必须大于0");
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(yyyy_MM_dd_T_HH_mm_SSSZ, Locale.getDefault());
        StringBuilder builder = new StringBuilder(sdf.format(new Date(time)));
        builder.insert(26, ":");
        return builder.toString();
    }

    /**
     * 将时间格式化为"2018-05-07 14:41:57",转换为手机设备所在时区
     *
     * @param time calenderTime
     * @return 格式化时间字符串
     */
    public static String timeToyyyy_MM_dd_HH_mm_ss(long time) {
        if (0 >= time) {
            Log.e(TAG, "convertToString : time 必须大于0");
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(yyyy_MM_dd_HH_mm_ss, Locale.getDefault());
        return sdf.format(new Date(time));
    }
    /**
     * 将时间格式化为"2018-05-07 14:41",转换为手机设备所在时区
     *
     * @param time calenderTime
     * @return 格式化时间字符串
     */
    public static String timeToyyyy_MM_dd_HH_mm(long time) {
        if (0 >= time) {
            Log.e(TAG, "convertToString : time 必须大于0");
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(yyyy_MM_dd_HH_mm, Locale.getDefault());
        return sdf.format(new Date(time));
    }








    /**
     * 将 "2018-05-07T14:41:57.819+03:00" 转换为 long型
     *
     * @param formatTime "2018-05-07T14:41:57.819+03:00"
     * @return long型
     */
    public static long yyyy_MM_dd_T_HH_mm_SSSZToLong(String formatTime) {
        Calendar calendar = yyyy_MM_dd_T_HH_mm_SSSZToCalendar(formatTime);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取当前时间
     *
     * @return 当前时间
     */
    public static String getPresentTime() {
        return getFormatTime("yyyy-MM-dd HH:mm:ss.SSS");
    }

    /**
     * 获取当前时间
     *
     * @return 当前时间
     */
    public static String getPresentTimeNoMS() {
        return getFormatTime("yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 获取当前时间
     *
     * @return 当前时间
     */
    public static String getPresentTimeNoSeconds() {
        return getFormatTime("yyyy/MM/dd HH:mm");
    }


    private static String getFormatTime(String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.getDefault());
        return sdf.format(Calendar.getInstance().getTime());
    }


    /**
     * 获取给定日期当天的开始时间:00:00:00
     * @param calendar
     * @return
     */
    public static Calendar getStartTime(Calendar calendar){
        calendar.set(Calendar.HOUR_OF_DAY,0 );
        calendar.set(Calendar.MINUTE,0 );
        calendar.set(Calendar.SECOND,0 );
        return calendar;
    }


    /**
     * 获取给定日期当天的结束时间:23:59:59
     * @param calendar
     * @return
     */
    public static Calendar getEndTime(Calendar calendar){
        calendar.set(Calendar.HOUR_OF_DAY,23 );
        calendar.set(Calendar.MINUTE,59 );
        calendar.set(Calendar.SECOND,59 );
        return calendar;
    }
}
